package {
	import org.molamil.acto.ext.perilla.PerillaController;
	import org.molamil.acto.core.events.ConfLoadEvent;
	import flash.errors.IllegalOperationError;
	import flash.display.DisplayObjectContainer;
	import org.molamil.acto.App;
	/**
	 * @author martindyrby
	 */
	public class Controller extends App {
		
		public static const APP_ID:String = "perillatest";
		public static const FLASHVARS_CONF_PATH:String = "confPath";
		public static var confPath:String = "acto.xml";


		public static var _instance : Controller;
		private var _perillaController : PerillaController;
		
		public function Controller(root:DisplayObjectContainer, params:Object = null) {
			if (params == null)
				params = new Object();
				
			params.id = APP_ID;
			
			super(root, params);
			
			if (_instance != null)
				throw new IllegalOperationError("Controller was already instantiated, use instance getter for further referentiation");
				
			_instance = this;
		}
		
		
		
		public function init():void {
			
			trace('init: ' + (init));
			var loadConfPath : String = confPath;
			
			// The flashvars have already been loaded in the props.
			
			if (props[FLASHVARS_CONF_PATH] != null)
				loadConfPath = props[FLASHVARS_CONF_PATH];

			addEventListener(ConfLoadEvent.COMPLETE, confCompleteHandler, false, 1, true);
			addEventListener(ConfLoadEvent.ERROR, confErrorHandler, false, 0, true);
			
			loadConf(loadConfPath);
			
		}
		
		private function confCompleteHandler(event:ConfLoadEvent):void {
			
			trace('confCompleteHandler: ' + (confCompleteHandler));
			_perillaController = new PerillaController(this);
			_perillaController.init();
		}
		
		private function confErrorHandler(event:ConfLoadEvent):void {
			trace("ERROR: The acto configuration couldn't be loaded");
		}
		
		
		public static function get instance():Controller {
			return _instance;
		}
		
		public function get perillaController():PerillaController {
			return _perillaController;
		}
		

	}
}
